<?php
namespace app\mobile\controller;

use app\common\model\RougeSystem;
use app\mobile\model\RougeShareSuccessLog;
use think\Exception;
use think\Request;
use think\Session;

class Login extends Base
{
    public function index(Request $request)
    {
        // clear session 测试第一次的登录情况
        /*$sessionM = new Session();
        $sessionM->clear();*/
        // END
        if ($request->isAjax()){
            //echo 'AJAX' . PHP_EOL;
            $model = new RougeSystem();
            $param = $request->post();
//            $init = Session::get();
            $map['uniacid'] = $param['uniacid'];
            $list = $model->where($map)->field('loading_url')->find();

            if ($list){
                $rarr['code'] = 0;
                $rarr['message'] = 'success';
                $rarr['data'] = $list;
            }else{
                $rarr['code'] = 9001;
                $rarr['message'] = 'error';
                $rarr['data'] = $list;
            }
            return json($rarr);
        }else {
            $url = $request->url(true);
            $this->init($url);
            $user['openid'] = '';
            $user = $this->getuserinfo();
            //var_dump($user);die;
            if (!$user){
                $user['platid'] = $request->param('platid');
                $user['uniacid'] = $user['platid'] ;
            }

            $this->assign('user',$user);
            //END
            $param = $request->param();
            if (isset($param['outer'])){
                $info['outer'] = $param['outer'];
                // 推荐处理
                $distributionM = new \app\mobile\model\Distribution();
                if (!$distributionM->addChild($param['outer'])) {
                    throw new Exception('服务器繁忙，请联系客服');
                }
                // END
            }else{
                $info['outer'] = '';
            }
            // 分享而引入的用户的逻辑处理
            if (isset($param['share_id'])) { // 分享进入的请求
                $shareSuccessLogM = new RougeShareSuccessLog();
                $shareSuccessLogM->addShare($param['share_id']);
            }
            // END

            $this->assign('info',$info);
            return $this->fetch();
        }
    }

    //获取code
    public function getuserauth(Request $request){

    }

    public function loguot(){
        Session::clear();
    }
}
